package com.cuiweiyou.uiautomator20;

import android.app.Instrumentation;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.RemoteException;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import android.support.test.uiautomator.By;
import android.support.test.uiautomator.BySelector;
import android.support.test.uiautomator.SearchCondition;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject2;
import android.support.test.uiautomator.Until;
import android.util.Log;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;

import jackmego.com.jieba_android.JiebaSegmenter;

/**
 * www.gaohaiyan.com
 */
@RunWith(AndroidJUnit4.class)
public class TestChatting {
    private static final String TAG = "ard";
    private static UiDevice device;
    private static Context context;
    private static ClipboardManager clipboard;

    @BeforeClass // 此方法须静态
    public static void beforeClass() throws RemoteException {
        UtilLogs.saveLog("1.BeforeClass");

        UtilFile.initSourcePath(); // chatting

        Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
        context = instrumentation.getTargetContext().getApplicationContext();

        clipboard = RootApplication.getClipboardManager();

        device = UiDevice.getInstance(instrumentation);

        JiebaSegmenter.init(context);

        if (!device.isScreenOn()) {
            device.wakeUp();
        }

        TestUtilCtrl.requestPermission(device);
        UtilSQLite.getInstance(context);
    }

    @Before
    public void before() {
        UtilLogs.saveLog("2.Before");

        String versionName = UtilPhone.getVersionName(context);

        String deviceSN = UtilPhone.getDeviceSN().toLowerCase();
        Constant.DEVICE_SN = deviceSN;

        UtilLogs.saveLog(" ");
        UtilLogs.saveLog(" ");
        UtilLogs.saveLog(" ");
        UtilLogs.saveLog("------ 开始聊天 ------");

        ManagerViewID.getInstance().initSelf(deviceSN, versionName);
        ManagerGimmick.getInstance().init();
        ManagerWord.getInstance().init();
    }

    @After
    public void after() {
        UtilLogs.saveLog("-2.After");
    }

    @AfterClass // 此方法须静态
    public static void afterClass() {
        UtilLogs.saveLog("-1.AfterClass");
    }

    /**
     * 遍历首页进行聊天
     */
    @Test
    public void testChatting() {
        TestUtilCtrl.launcherWechatApp(device, "遍历首页进行聊天");
        TestUtilCtrl.switchTab(device, "微信");

        scrollChattingList(); // 启动聊天，聊后保留

        UtilLogs.saveLog("聊天完毕-------");
    }

    // 划动列表查找新通过的好友
    private void scrollChattingList() {
        String home_wechat_tab = ManagerViewID.getInstance().getSelfViewId(ManagerViewID.VIEW_ID_NAME_home_wechat_tab);
        UiObject2 d9_;
        if (null == home_wechat_tab) {
            d9_ = device.findObject(By.res("com.tencent.mm", "cw4")); // d9_"微信"右上角数字角标
            UtilLogs.saveLog("查找首页底部tab上的数字控件 cw4");
        } else {
            String id = home_wechat_tab.replace("com.tencent.mm:id/", "");
            d9_ = device.findObject(By.res("com.tencent.mm", id));
            UtilLogs.saveLog("查找首页底部tab上的数字控件 id=" + id);
        }
        if (null != d9_) {
            UtilLogs.saveLog("有新消息");

            // [0,1255][180,1344]
            int x = ((180 - 0) / 2) + 0;
            int y = ((1344 - 1255) / 2) + 1344;
            TestUtilCtrl.doubleClick(d9_);
            UtilLogs.saveLog("双击位置：x=" + x + "，y=" + y);

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            String home_chat_litem = ManagerViewID.getInstance().getSelfViewId(ManagerViewID.VIEW_ID_NAME_home_wechat_list_item);
            BySelector nf;
            if (null == home_chat_litem) {
                nf = By.res("com.tencent.mm", "lv"); // nf 聊天列表item上朋友头像右上角数字角标
                UtilLogs.saveLog("查找列表好友头像上的数字 lv");
            } else {
                String id = home_chat_litem.replace("com.tencent.mm:id/", "");
                nf = By.res("com.tencent.mm", id);
                UtilLogs.saveLog("查找列表好友头像上的数字 id=" + id);
            }

            SearchCondition<Boolean> condition = Until.hasObject(nf);
            Boolean wait = device.wait(condition, 2000);
            UtilLogs.saveLog("是否找到新消息item：" + wait);
            if (wait) {
                UtilLogs.saveLog("找到新消息item");

                UiObject2 nfObject = device.findObject(nf);// 朋友头像右上角的数字角标

                TestUtilChat.doChatting(context, clipboard, device, nfObject, "", true); // keep模式聊天
                UtilLogs.saveLog("聊天结束");

                device.pressBack(); // 返回首页
                UtilLogs.saveLog("返回首页列表");
            }

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            scrollChattingList();
        }
        UtilLogs.saveLog("没--有--新消息");
    }
}
